package com.dou.ourpro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dou.ourpro.pojo.Users;
import com.dou.ourpro.service.UsersService;
import com.dou.ourpro.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.dou.ourpro.controller.loginController.usernames;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/11/2021/11/10/12:14
 * @Description:
 */
@Controller
@RequestMapping("/view")
public class viewController {
    @Autowired
    loginService loginService;
    @Autowired
    UsersService usersService;
    @GetMapping("/list")
    public String userList(Model model){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_grade", 0);
        List<Users> list = usersService.list(queryWrapper);
        model.addAttribute("emps",list);
        return "list/member-list";
    }

    @GetMapping("/adminlist")
    public String adminList(Model model){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.ne("user_grade", 0);
        List<Users> list = usersService.list(queryWrapper);
        model.addAttribute("emps",list);
        model.addAttribute("usernames",usernames);
        return "admin/admin-list";
    }


    @GetMapping("/search")
    public String searchList(@RequestParam String name, Model model){
        QueryWrapper<Users> queryWrapper=new QueryWrapper();
        queryWrapper.eq("name",name);
        List<Users> list = usersService.list(queryWrapper);
        model.addAttribute("emps",list);

        return "article/article-list";
    }

    @GetMapping("/useradd")
    public String useradd(){

        return "list/member-add";
    }
    @GetMapping("/adminadd")
    public String adminadd(){

        return "admin/admin-add";
    }
    @GetMapping("/useredit")
    public String useredit(){
        return "list/member-edit";
    }

    @GetMapping("/adminedit")
    public String adminedit(){
        return "admin-edit";
    }

    @GetMapping("/userdelete")
    public String userdelete(@RequestParam("userId") int userId){
        System.out.println(userId);
        QueryWrapper<Users> queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        usersService.remove(queryWrapper);
        return "redirect:/view/list";
    }

    @PostMapping("/add")
    public String useradd(Users users){
        System.out.println(users);
        usersService.save(users);
        return "redirect:/view/list";
    }


    @PostMapping("/edit")
    public String useredit(Users users){
        System.out.println(users);
        QueryWrapper<Users> queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",users.getUserId());
        usersService.update(users,queryWrapper);
        return "redirect:/view/list";
    }
}
